package com.fingard.dsp.bank.directbank.pos01;

import java.util.HashMap;
import java.util.Map;

import cfca.sadk.org.bouncycastle.crypto.digests.SM3Digest;
import cfca.sadk.org.bouncycastle.crypto.macs.HMac;
import cfca.sadk.org.bouncycastle.crypto.params.KeyParameter;
import cfca.sadk.org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import com.fingard.dsp.bank.directbank.bankInterface.IOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.ReqOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.RetAtsOtherElse;
import com.fingard.text.MD5SaltHelper;
import com.fingard.text.StringHelper;

/**
 * POS通用线路POS统一签名接口
 * 
 * @author Cruise
 * @date 2019/3/18
 */
public class POS01OtherElse extends POS01Base implements IOtherElse {
	@Override
	public RetAtsOtherElse d_else(ReqOtherElse p_reqElse) throws Exception {
		String transType = p_reqElse.reqHeader.transType;
		if ("UnifiedSign".equals(transType)) {
			if ("POS".equals(p_reqElse.reqHeader.commandCode)) {
				return unifiedSignForPos(p_reqElse);
			}
		} else {
			RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
			retAts.retHashMap = new HashMap<String, String>();
			retAts.retHashMap.put("RespCode", "-1");
			retAts.retHashMap.put("RespInfo", "未知的TransType：" + transType);
			return retAts;
		}
		return null;
	}

	/**
	 * POS保单信息查询签名
	 * 
	 * @param p_reqElse
	 * @return
	 */
	public RetAtsOtherElse unifiedSignForPos(ReqOtherElse p_reqElse) {
		RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
		retAts.retHashMap = new HashMap<String, String>();

		try {
			Map<String, String> mapParams = p_reqElse.reqHashMap;
			String POSRecChannel = p_reqElse.reqHashMap.get("POSRecChannel");
			
			String saltKey = getBankConfig().getMapValueByKey("saltKeyMapping", p_reqElse.reqHashMap.get("POSRecChannel"));
			WriteBankLogLn2("当前渠道POSRecChannel:[" + POSRecChannel + "]MD5密钥为:[" + saltKey + "].");
			
			mapParams = removeHead(mapParams);

			String tmpSignData = getSignStr(mapParams);
			tmpSignData += "&key=" + saltKey;
			WriteBankLogLn2("待签名数据:\r\n" + tmpSignData);
			String sign = "";
			if(POSRecChannel.equals("bjums")){
				sign = getSm3Sign(tmpSignData,"UTF-8",saltKey);
			}else{
				sign = MD5SaltHelper.md5(tmpSignData, getCharset());
			}

			WriteBankLogLn2("签名结果:" + sign);

			if (StringHelper.hasAnyChar(sign)) {
				retAts.retHashMap.put("RespCode", "0");
				retAts.retHashMap.put("RespInfo", "签名成功!");
				retAts.retHashMap.put("Result", sign);
			} else {
				retAts.retHashMap.put("RespCode", "-1");
				retAts.retHashMap.put("RespInfo", "签名失败!");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.retHashMap.put("RespCode", "-1");
			retAts.retHashMap.put("RespInfo", ex.getMessage());
			WriteBankLogLn(ex);
		}

		return retAts;
	}
	public String  getSm3Sign(String signStr, String encoding, String key) {
		try{
			byte[] bytes = signStr.getBytes(encoding);
			KeyParameter keyParameter = new KeyParameter(key.getBytes());
			SM3Digest digest = new SM3Digest();
			HMac mac = new HMac(digest);
			mac.init(keyParameter);
			mac.update(bytes, 0, bytes.length);
			byte[] result = new byte[mac.getMacSize()];
			mac.doFinal(result, 0);
			String hexString = ByteUtils.toHexString(result);
			return hexString;



		}catch(Exception e){
			e.printStackTrace();
		}
		return signStr;
	}
}
